﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.IService.Common;
using DormMS.Model;
using DormMS.Model.SearchModels;
using DormMS.Service;
using Infrastructure;
using NLog;

namespace DormMS.Controllers
{
    public class BuildingController : BaseController
    {
        protected IBuildingService buildingService;
        protected ILogService logService;
        protected IUserInfoService userInfoService;
        private static Logger NLog = LogManager.GetLogger("Request");
        /// <summary>
        /// 初期化
        /// </summary>
        /// <param name="s"></param>
        /// <param name="_buildingService"></param>
        /// <param name="l"></param>
        public BuildingController(IUserInfoService s, IBuildingService _buildingService, ILogService l) : base(s)
        {
            buildingService = _buildingService;
            logService = l;
            userInfoService = s;
        }
        /// <summary>
        /// 获取楼宇列表
        /// </summary>
        /// <returns></returns>
        public ActionResult BuildingList()
        {
            return View();
        }
        
        /// <summary>
        /// 获取楼宇列表期初数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetBuildingList(BuildingSearchModel search)
        {
            int total = 0;
            var list = buildingService.GetBuildingList(search, ref total);
            return Json(new { Total = total, Results = list , _UserInfo = userInfoService.GetUserInfo(CurrentLoginUser.Id) });
        }

        /// <summary>
        /// 获取楼宇明细
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult BuildingDetail()
        {
            return View();
        }
        /// <summary>
        /// 获取楼宇初期化明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetBuildingInitDetail(int id)
        {
            return Json(new { buindingInfo = buildingService.GetBuildingInfoById(id) });
        }

        /// <summary>
        /// 楼宇保存CHECK
        /// </summary>
        /// <param name="BuildingNO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult BuildingCheck(string BuildingNO)
        {
            Result result = new Result();
            var entity = buildingService.GetBuildingInfoByNO(BuildingNO);

            if (entity == null)
            {
                result.isSuccess = true;
            }
            return Json(result);
        }

        /// <summary>
        /// 新增修改大楼信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult BuildingSave(Building model)
        {
            Result result = new Result();
            try
            {
                
                var entity = buildingService.GetBuildingInfoById(model.Id);
                int rstcount = 0;
                if (entity == null)
                {
                    model.AddUserId = CurrentLoginUser.Id;
                    model.AddTime = DateTime.Now;
                    rstcount = buildingService.BuildingAdd(model);
                    result.isSuccess = true;

                }
                else
                {
                    entity.BuildingNO = model.BuildingNO;
                    entity.Address = model.Address;
                    entity.EditUserId = CurrentLoginUser.Id;
                    entity.EditTime = DateTime.Now;
                    rstcount = buildingService.BuildingEdit(model);                    
                    result.isSuccess = true;
                    
                }
            }
            catch(Exception ex)
            {
                result.msg = "保存大楼信息失败！";
                NLog.Debug("保存大楼信息失败：" + ex.Message + "，" + ex.StackTrace);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除大楼信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult BuildingDel(Building model)
        {
            Result result = new Result();
            var entity = buildingService.GetBuildingInfoById(model.Id);

            if (entity != null)
            {
                model.IsDeleted = true;
                buildingService.BuildingDel(model);
                result.isSuccess = true;
            }
            else
            {
                result.msg = "大楼信息不存在！";
            }
            return Json(result);
        }

        public ActionResult AdjustFlat()
        {
            return View();
        }

        public ActionResult AdjustmentAdd()
        {
            return View();
        }

        public ActionResult GetV_UserInfo(string name)
        {
            Result result = new Result();
            var entity = buildingService.GetV_UserInfo(name);

            if (entity != null)
            {
                result.data = entity;
                result.isSuccess = true;
            }
            else
            {
                result.msg = "学生信息不存在！";
            }

            return Json(result);
        }
    }
}